#include "TableRowCellGroup.h"
#include "../../Utility/XXlogger.h"
#include "LabelCell.h"
#include <QGenericArgument>
#include "../../XXqtDefine.h"

TableRowCellGroup::TableRowCellGroup(QTableWidget *tableWidget, int row, const QList<ColumnInfo> &columnInfoList, const QVariantMap &data)
    : QObject (tableWidget){
    //_row = row;
    _columnInfoList = columnInfoList;
    _data = data;
    installCell(tableWidget,row);
    setData(data);
}
TableRowCellGroup::~TableRowCellGroup(){
}

void TableRowCellGroup::updateData(const QVariantMap &data){
    //xxLogStr("[1]\n"+XXqtDefine::variantToJsonString(_data,true));
    for (auto iter=data.begin(); iter!=data.end(); iter++) {
        _data[iter.key()] = iter.value();
    }
    //xxLogStr("[2]\n"+XXqtDefine::variantToJsonString(_data,true));
    foreach(auto info,_columnInfoList){
        QString name = info._name;
        CellBase *cell = _nameToCell.value(name,nullptr);
        if(!data.contains(name)){
            continue;
        }

        cell->updateData(data.value(name));
        _data[name] = cell->getData();
    }
    //xxLogStr("[3]\n"+XXqtDefine::variantToJsonString(_data,true));
}
void TableRowCellGroup::setData(const QVariantMap &data){
    //xxLogStr("[1]\n"+XXqtDefine::variantToJsonString(_data,true));
    _data = data;
    //xxLogStr("[2]\n"+XXqtDefine::variantToJsonString(_data,true));
    foreach(auto info,_columnInfoList){
        QString name = info._name;
        CellBase *cell = _nameToCell.value(name,nullptr);
        if(!data.contains(name)){
            continue;
        }

        cell->setData(data.value(name));
        _data[name] = cell->getData();
    }
    //xxLogStr("[3]\n"+XXqtDefine::variantToJsonString(_data,true));
}
QVariantMap TableRowCellGroup::getData(){
    return _data;
}
void TableRowCellGroup::delData(){
    for (auto iter=_columnInfoList.begin(); iter<_columnInfoList.end(); iter++) {
        CellBase *cell = _nameToCell.value(iter->_name,nullptr);
        if(nullptr == cell){
            continue;
        }
        delete cell;
    }
}

void TableRowCellGroup::setSelected(bool selected){
    foreach(auto info,_columnInfoList){
        QString name = info._name;
        CellBase *cell = _nameToCell.value(name,nullptr);
        cell->setSelected(selected);
    }
}
bool TableRowCellGroup::isSelected(){
    return _nameToCell.values().first()->isSelected();
}
void TableRowCellGroup::setActived(bool actived){
    foreach(auto info,_columnInfoList){
        QString name = info._name;
        CellBase *cell = _nameToCell.value(name,nullptr);
        cell->setActived(actived);
    }
}
bool TableRowCellGroup::isActived(){
    return _nameToCell.values().first()->isActived();
}

void TableRowCellGroup::installCell(QTableWidget *tableWidget, int row){
    for(int col=0; col<_columnInfoList.count(); col++){
        auto columnInfo = _columnInfoList[col];
        auto name = columnInfo._name;
        auto cellBlock = columnInfo._cellBlock;
        auto cellMeta = columnInfo._cellMeta;

        QObject *object = nullptr!=cellBlock ? cellBlock() : cellMeta.newInstance();
        CellBase *cell = qobject_cast<CellBase*>(object);
        if(nullptr == cell){
            xxErrStr(QString("could not new cell. columnIndex:%1 columnName:%2").arg(col).arg(name));
            continue;
        }

        cell->setParent(tableWidget);
        cell->setObjectName(name);
        cell->setData(_data.value(name));
        connect(cell,&CellBase::sigEvent,this,&TableRowCellGroup::sigEvent);
        connect(cell,&CellBase::sigDataChanged,this,&TableRowCellGroup::onDataChanged);
        _nameToCell[name] = cell;

        QTableWidgetItem *item = tableWidget->item(row,col);
        if(nullptr == item){
            item = new QTableWidgetItem;
            item->setFlags(Qt::ItemFlag::ItemIsEnabled|Qt::ItemFlag::ItemIsSelectable);
            tableWidget->setItem(row,col,item);
        }
        tableWidget->setCellWidget(row,col,cell);
    }
}
void TableRowCellGroup::onDataChanged(){
    CellBase *cell = qobject_cast<CellBase*>(sender());
    QString colKey = _nameToCell.key(cell,"");
    if(colKey.isEmpty()){
        xxErr("could not find key for cell.");
        return;
    }
    auto data = cell->getData();
    _data[colKey] = data;
    emit sigDataChanged();
}
